﻿using System;
using System.Collections.Generic;
using System.Text;
using DataProvider;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLayer
{
    public class PNhap: XL_Bang
    {
        #region Cac thuoc tinh
        private string _soPN;
        public string SoPN
        {
            get { return _soPN; }
            set { _soPN = value; }
        }
        private DateTime _ngayDat;
        public DateTime NgayDat
        {
            get { return _ngayDat; }
            set { _ngayDat = value; }
        }
        private string _soDH;
        public string SoDH
        {
            get { return _soDH; }
            set { _soDH = value; }
        }
        #endregion


        #region cac phuong thuc
        public DataTable GetData()
        {
            StrSQL = "select * from PNHAP";
            return GetDataTable();
        }
        public bool AddNew()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_PNHAP";
                command.Parameters.AddWithValue("@SOPN", _soPN);
                command.Parameters.AddWithValue("@NGAYNHAP", _ngayDat.ToString());
                command.Parameters.AddWithValue("@SODH", _soDH);
                command.Parameters.AddWithValue("@TYPE", 0);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                close();
                command.Dispose();
            }
        }
        public bool Edit()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_PNHAP";
                command.Parameters.AddWithValue("@SOPN", _soPN);
                command.Parameters.AddWithValue("@NGAYNHAP", _ngayDat.ToString());
                command.Parameters.AddWithValue("@SODH", _soDH);
                command.Parameters.AddWithValue("@TYPE", 1);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }
        public bool Delete()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_PNHAP";
                command.Parameters.AddWithValue("@SOPN", _soPN);
                command.Parameters.AddWithValue("@NGAYNHAP", string.Empty);
                command.Parameters.AddWithValue("@SODH", string.Empty);
                command.Parameters.AddWithValue("@TYPE", 2);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }

        public DataTable GetData(DateTime fromDate, DateTime toDate, string selectedMaVTU)
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "sp_TONGNHAP";
                command.Parameters.AddWithValue("@FROMDATE", fromDate);
                command.Parameters.AddWithValue("@TODATE", toDate);
                command.Parameters.AddWithValue("@MAVTU", selectedMaVTU);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                return table;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                close();
                command.Dispose();
            }
        }
        #endregion

       
    }
}
